package com.moorecat.app;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.provider.Settings;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView.OnEditorActionListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.moorecat.MooerApplication;
import com.moorecat.R;
import com.moorecat.config.Const;
import com.moorecat.utils.ui.BitmapViewBinder;
import com.moorecat.utils.ui.UiUtils;

public class Near extends Activity implements OnItemClickListener,
		OnClickListener {

	private MooerApplication application;

	private ListView listView;

	private ImageView searchButton;

	private TextView address;

	private EditText edit;

	private List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

	private SimpleAdapter adapter;

	private Handler handler = new Handler();

	private Runnable locationMonitor = new Runnable() {

		@Override
		public void run() {
			if (!application.getAddress().equals(address.getText().toString())) {
				address.setText(application.getAddress());
			}
			handler.postDelayed(locationMonitor, 10000);

		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.near);
		application = (MooerApplication) getApplication();
		listView = (ListView) findViewById(R.id.listview);
		searchButton = (ImageView) findViewById(R.id.search_btn);
		address = (TextView) findViewById(R.id.address);
		edit = (EditText) findViewById(R.id.search);
		adapter = new SimpleAdapter(this, list, R.layout.near_item,
				new String[] { "icon", "name" }, new int[] { R.id.icon,
						R.id.name });
		adapter.setViewBinder(new BitmapViewBinder());
		listView.setAdapter(adapter);
		if (application.getAddress().length() > 0) {
			address.setText(application.getAddress());
		} else {
			address.setText(application.getLatitude() + ","
					+ application.getLongitude());
		}
		initListView();
		listView.setOnItemClickListener(this);
		searchButton.setOnClickListener(this);
		edit.setOnEditorActionListener(new OnEditorActionListener() {
			
			@Override
			public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
				if (edit.getText().toString().length() > 0) {
					Intent intent = new Intent(Near.this, BusinessList.class);
					intent.putExtra("type1", "附近");
					intent.putExtra("type2", "搜索");
					intent.putExtra("content", edit.getText().toString());
					startActivity(intent);
				} else {
					UiUtils.showToast(Near.this, "请输入您要查询的关键字");
				}
				return false;
			}
		});
	}

	private void initListView() {
		Resources resources = getResources();
		Map<String, Object> map1 = new HashMap<String, Object>();
		map1.put("icon", resources.getDrawable(R.drawable.list_food));
		map1.put("name", Const.types.get(0));
		Map<String, Object> map2 = new HashMap<String, Object>();
		map2.put("icon", resources.getDrawable(R.drawable.list_beauty));
		map2.put("name", Const.types.get(1));
		Map<String, Object> map3 = new HashMap<String, Object>();
		map3.put("icon", resources.getDrawable(R.drawable.list_marry));
		map3.put("name", Const.types.get(2));
		Map<String, Object> map4 = new HashMap<String, Object>();
		map4.put("icon", resources.getDrawable(R.drawable.list_parenting));
		map4.put("name", Const.types.get(3));
		Map<String, Object> map5 = new HashMap<String, Object>();
		map5.put("icon", resources.getDrawable(R.drawable.list_sports));
		map5.put("name", Const.types.get(4));
		Map<String, Object> map6 = new HashMap<String, Object>();
		map6.put("icon", resources.getDrawable(R.drawable.list_education));
		map6.put("name", Const.types.get(5));
		Map<String, Object> map7 = new HashMap<String, Object>();
		map7.put("icon", resources.getDrawable(R.drawable.list_play));
		map7.put("name", Const.types.get(6));
		Map<String, Object> map8 = new HashMap<String, Object>();
		map8.put("icon", resources.getDrawable(R.drawable.list_shopping));
		map8.put("name", Const.types.get(7));
		Map<String, Object> map9 = new HashMap<String, Object>();
		map9.put("icon", resources.getDrawable(R.drawable.list_hotel));
		map9.put("name", Const.types.get(8));
		Map<String, Object> map10 = new HashMap<String, Object>();
		map10.put("icon", resources.getDrawable(R.drawable.list_service));
		map10.put("name", Const.types.get(9));
		Map<String, Object> map11 = new HashMap<String, Object>();
		map11.put("icon", resources.getDrawable(R.drawable.list_more));
		map11.put("name", Const.types.get(10));
		list.add(map1);
		list.add(map2);
		list.add(map3);
		list.add(map4);
		list.add(map5);
		list.add(map6);
		list.add(map7);
		list.add(map8);
		list.add(map9);
		list.add(map10);
		list.add(map11);

	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position,
			long arg3) {
		System.out.println(position);
		Intent intent = new Intent();
		intent.putExtra("type1", "附近");
		intent.putExtra("typePosition", position);
		if (position == 0) {
			intent.setClass(Near.this, BusinessList.class);
			intent.putExtra("type2", "美食");
			intent.putExtra("categoryId", Const.MEISHI);
			startActivity(intent);
		} else if (position == 1) {
			intent.setClass(Near.this, BusinessList.class);
			intent.putExtra("type2", "丽人");
			intent.putExtra("categoryId", Const.LIREN);
			startActivity(intent);
		} else if (position == 2) {
			intent.setClass(Near.this, BusinessList.class);
			intent.putExtra("type2", "花嫁");
			intent.putExtra("categoryId", Const.HUAJIA);
			startActivity(intent);
		} else if (position == 3) {
			intent.setClass(Near.this, BusinessList.class);
			intent.putExtra("type2", "亲子");
			intent.putExtra("categoryId", Const.QINZI);
			startActivity(intent);
		} else if (position == 4) {
			intent.setClass(Near.this, BusinessList.class);
			intent.putExtra("type2", "健身");
			intent.putExtra("categoryId", Const.JIANSHEN);
			startActivity(intent);
		} else if (position == 5) {
			intent.setClass(Near.this, BusinessList.class);
			intent.putExtra("type2", "培训");
			intent.putExtra("categoryId", Const.PEIXUN);
			startActivity(intent);
		} else if (position == 6) {
			intent.setClass(Near.this, BusinessList.class);
			intent.putExtra("type2", "休闲娱乐");
			intent.putExtra("categoryId", Const.YOUXIANYULE);
			startActivity(intent);
		} else if (position == 7) {
			intent.setClass(Near.this, BusinessList.class);
			intent.putExtra("type2", "购物");
			intent.putExtra("categoryId", Const.GOUWU);
			startActivity(intent);
		} else if (position == 8) {
			intent.setClass(Near.this, BusinessList.class);
			intent.putExtra("type2", "酒店");
			intent.putExtra("categoryId", Const.JIUDIAN);
			startActivity(intent);
		} else if (position == 9) {
			intent.setClass(Near.this, BusinessList.class);
			intent.putExtra("type2", "生活服务");
			intent.putExtra("categoryId", Const.SHENGHUOFUWU);
			startActivity(intent);
		} else if (position == 10) {
			intent.setClass(Near.this, NearMore.class);
			startActivity(intent);
		}

	}

	@Override
	public void onClick(View v) {
		if (v == searchButton) {
			if (edit.getText().toString().length() > 0) {
				Intent intent = new Intent(this, BusinessList.class);
				intent.putExtra("type1", "附近");
				intent.putExtra("type2", "搜索");
				intent.putExtra("content", edit.getText().toString());
				startActivity(intent);
			} else {
				UiUtils.showToast(this, "请输入您要查询的关键字");
			}
		}

	}

	@Override
	protected void onStop() {
		super.onStop();
		handler.removeCallbacks(locationMonitor);
	}

	@Override
	protected void onStart() {
		super.onStart();
		handler.postDelayed(locationMonitor, 10000);
	}

	@Override
	protected void onResume() {
		super.onResume();
		if (edit != null) {
			edit.setText("");
		}
        //判断是否获取到经纬度，如果不能则提示
        if (application.getLatitude() == 0.0 && application.getLongitude() == 0.0) {
            LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
            if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
                AlertDialog.Builder ad = new AlertDialog.Builder(this);
                ad.setTitle(R.string.warn);
                ad.setMessage(R.string.gps_warn);
                ad.setPositiveButton(R.string.setting_gps, new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Intent intent = new Intent();
                        intent.setAction(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                        startActivity(intent);
                    }
                });
                ad.setNeutralButton(R.string.back, new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                           Near.this.finish();
                    }
                });
                ad.show();
            }

        }
	}

}
